class Solution {
public:
    vector<int> path;
    vector<vector<int>> result;
    void dfs(vector<int> &nums, int index, vector<bool> &used) {
        result.push_back(path);
        if (index == nums.size()) return ;
        for (int i = index; i < nums.size(); i++) {
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) continue;
            used[i] = true;
            path.push_back(nums[i]);
            dfs(nums, i + 1, used);
            path.pop_back();
            used[i] = false;
        }
        return ;
    }
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<bool> used(nums.size(), false);
        dfs(nums, 0, used);
        return result;
    }
};
